﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DingTalkAgent.Web.Controllers
{
    [Common.Filter.AuthorizeFilter]
    public class SysBaseController : Common.adminBaseController
    {
        private DingTalkAgent.SqlData.SysPage ISysPage = new SqlData.SysPage();
        private DingTalkAgent.SqlData.SysAction ISysAction = new SqlData.SysAction();
        private DingTalkAgent.SqlData.SysNav ISysNav = new DingTalkAgent.SqlData.SysNav();

            
        #region SysPage
        //获取列表信息
        [Common.Filter.ViewPage]
        public ActionResult SysPList(string Code = "001", bool isPopup = false, bool getChild = true)
        {
            try
            {
                int Site_ID = 1;
                int records = 0;

                    if (Request.IsAjaxRequest())
                {
                    int rows = Convert.ToInt32(Request["rows"] == null ? "10" : Request["rows"].ToString());
                    int page = Convert.ToInt32(Request["page"] == null ? "1": Request["page"].ToString());
                    var keyword = Request["keyword"] == null ? "" : Request["keyword"].ToString();

                    List<DingTalkAgent.Model.SysPage> list = ISysPage.GetPageList(page, rows, out records, Site_ID, keyword, Code, getChild);
                    var pagelist = new Models.SysP_Page()
                    {
                        records = records,
                        page = page,
                        pagesize = rows,
                        List = list,
                    };

                    return Json(pagelist);
                }

                ViewBag.Code = Code;
                if (!isPopup)
                {
                    return View();
                }
                else
                {
                    return View("PopupSysPList");
                }
    
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysPList", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        //添加信息
        public ActionResult SysPAdd(int Site_ID = 1)
        {
            return View();
        }
        [HttpPost]
        public ActionResult SysPAdd(DingTalkAgent.Model.SysPage model)
        {
            try
            {
                var code = string.Empty;
                string ParentCode = string.Empty;
                model.Site_ID = 1;
                //没选择的表示指定站点的最高级
                if (model.Parent_ID == -1)
                {
                    // ParentCode=Request.Form["keyword"] == null ? "" : Request.Form["keyword"].ToString();//
                    if (model.Site_ID < 10)
                    {
                        ParentCode = "00" + model.Site_ID;
                    }
                    else if (model.Site_ID < 100)
                    {
                        ParentCode = "0" + model.Site_ID;
                    }
                    else
                    {
                        ParentCode = "" + model.Site_ID;
                    }
                    model.Code = ISysPage.PageCode(ParentCode);
                }
                else
                {
                    ParentCode = Request.Form["ParentCode"].ToString();//
                    model.Code = ISysPage.PageCode(ParentCode);
                }
                if (ISysPage.Add(model) > 0)
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "添加页面", "成功添加页面:" + model.Name, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysPAdd", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }

        }
        //修改信息
        public ActionResult SysPEdit(int id)
        {
            var m = ISysPage.GetModel(id);
            if (m != null)
            {
                var a = ISysPage.GetModel(m.Parent_ID);
                ViewData["ParentName"] = a == null ? "" : a.Name;
                ViewData["ParentCode"] = a == null ? "" : a.Code;
            }
            return View(m);
        }

        [HttpPost]
        public ActionResult SysPEdit(DingTalkAgent.Model.SysPage model)
        {
            try
            {
                var code = string.Empty;
                string ParentCode = string.Empty;
                if (model.Page_ID == model.Parent_ID)
                {
                    return Content("不能修改成当前的子集!");
                }
                model.Site_ID = 1;
                //没选择的表示指定站点的最高级
                if (model.Parent_ID == -1)
                {
                    // ParentCode=Request.Form["keyword"] == null ? "" : Request.Form["keyword"].ToString();//
                    if (model.Site_ID < 10)
                    {
                        ParentCode = "00" + model.Site_ID;
                    }
                    else if (model.Site_ID < 100)
                    {
                        ParentCode = "0" + model.Site_ID;
                    }
                    else
                    {
                        ParentCode = "" + model.Site_ID;
                    }
                    model.Code = ISysPage.PageCode(ParentCode);
                }
                else
                {
                    ParentCode = Request.Form["ParentCode"].ToString();//
                    model.Code = ISysPage.PageCode(ParentCode);
                }
                if (ISysPage.Update(model))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "修改页面", "成功修改页面:" + model.Name, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败！");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysPEdit", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        //删除信息
        [HttpPost]
        public ActionResult SysPDelete(int id)
        {
            try
            {
                if (ISysPage.Delete(id))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "删除页面", "成功删除页面ID:" + id, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {

                    return Content("删除信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysPDelete", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        #endregion

        #region SysAction
        //获取列表信息
        [Common.myCommon]
        public ActionResult SysAList(int Page_ID, int PageIndex = 1)
        {
            try
            {
                int records = 0;
                int P_ID = 0 ;
                if (Page_ID > 0)
                {
                    P_ID = Page_ID;
                }
                if (Request.IsAjaxRequest())
                {
                    int rows = Convert.ToInt32(Request["rows"] == null ? "10" : Request["rows"].ToString());
                    int page = Convert.ToInt32(Request["page"] == null ? "1" : Request["page"].ToString());
                    var keyword = Request["keyword"] == null ? "" : Request["keyword"].ToString();

                    P_ID = Request["Page_ID"] == null ? 0 : int.Parse(Request["Page_ID"].ToString());

                    List<DingTalkAgent.Model.SysAction> list = ISysAction.GetPageList(page, rows, out records, P_ID);
                    var pagelist = new Models.SysA_Page()
                    {
                        records = records,
                        page = page,
                        pagesize = rows,
                        List = list,
                    };

                    return Json(pagelist);
                }
                var m = ISysPage.GetModel(P_ID);
                ViewBag.Name = m.Name;
                ViewBag.Page_ID = P_ID;

                return View();
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysAList", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        [Common.myCommon]
        //添加信息
        public ActionResult SysAAdd(int Page_ID)
        {
            ViewBag.Page_ID = Page_ID;
            return View();
        }
        [Common.myCommon]
        [HttpPost]
        public ActionResult SysAAdd(DingTalkAgent.Model.SysAction model)
        {
            try
            {
                if (ISysAction.Add(model) > 0)
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "添加动作", "成功添加动作:" + model.Act_Name, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysAAdd", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }

        }
        [Common.myCommon]
        //修改信息
        public ActionResult SysAEdit(int id)
        {
            var m = ISysAction.GetModel(id);
            return View(m);
        }
        [Common.myCommon]
        [HttpPost]
        public ActionResult SysAEdit(DingTalkAgent.Model.SysAction model)
        {
            try
            {
                if (ISysAction.Update(model))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "修改动作", "成功修改动作:" + model.Act_Name, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败！");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysAEdit", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        [Common.myCommon]
        //删除信息
        [HttpPost]
        public ActionResult SysADelete(int id)
        {
            try
            {
                if (ISysAction.Delete(id))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "删除动作", "成功删除动作ID:" + id, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("删除信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysADelete", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        #endregion

        #region SysNav

        //获取列表信息
        [Common.Filter.ViewPage]
        public ActionResult SysNList(int PageIndex = 1)
        {
            try
            {
                int records = 0;
                int Site_ID = 1;
                if (Request.IsAjaxRequest())
                {
                    int rows = Convert.ToInt32(Request["rows"] == null ? "10" : Request["rows"].ToString());
                    int page = Convert.ToInt32(Request["page"] == null ? "1" : Request["page"].ToString());
                    var keyword = Request["keyword"] == null ? "" : Request["keyword"].ToString();
                    int Parent_ID = Request.Form["Parent_ID"] == null | Request.Form["Parent_ID"].ToString() == "j1_1" ? -1 : Convert.ToInt32(Request.Form["Parent_ID"].ToString());

                    List<DingTalkAgent.Model.SysNav> list = ISysNav.GetPageList(page, rows, out records, Site_ID, Parent_ID, keyword);
                    var pagelist = new Models.SysN_Page()
                    {
                        records = records,
                        page = page,
                        pagesize = rows,
                        List = list,
                    };

                    return Json(pagelist);
                }
                return View();
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysNList", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }

        [Common.myCommon]
        public ActionResult SysNGetJsonList()
        {
            try
            {
                int total = 0;
                int Site_ID = 1;
                List<DingTalkAgent.Model.SysNav> list = ISysNav.GetPageList(1, 1000, out total, Site_ID, -1, "");
                var m = new DingTalkAgent.Model.SysNav() { id = -1, text = "菜单栏", Parent_ID = -1 };
                getTreeSysNav(list, m, -1);
                var l = new List<DingTalkAgent.Model.SysNav>() { m };
                return Json(l, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/Department/GetJsonList", ex.Message, _view_Admin_Info.Name, "");
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        //添加信息
        public ActionResult SysNAdd(int id)
        {
            ViewBag.Parent_ID = id;
            return View();
        }
        [HttpPost]
        public ActionResult SysNAdd(DingTalkAgent.Model.SysNav model)
        {
            try
            {
                model.Site_ID = 1;
                if (ISysNav.Add(model) > 0)
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "添加栏目", "成功添加栏目:" + model.Name, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysNAdd", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }

        }
        //修改信息
        public ActionResult SysNEdit(int id)
        {
            var m = ISysNav.GetModel(id);
            if (m.Page_ID > 0)
            {
                m.PageName = ISysPage.GetModel(m.Page_ID).Name;
            }
            return View(m);
        }

        [HttpPost]
        public ActionResult SysNEdit(DingTalkAgent.Model.SysNav model)
        {
            try
            {
                model.Site_ID = 1;
                if (ISysNav.Update(model))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "修改栏目", "成功修改栏目:" + model.Name, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("保存信息失败！");
                }
            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysNEdit", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        //删除信息
        [HttpPost]
        public ActionResult SysNDelete(int id)
        {
            try
            {
                if (ISysNav.Delete(id))
                {
                    //操作记录
                    Models.Operation.Add(_view_Admin_Info.UserID.ToString(), "删除栏目", "成功删除栏目id:" + id, _view_Admin_Info.Name);
                    return Content("true");
                }
                else
                {
                    return Content("删除信息失败!");
                }

            }
            catch (Exception ex)
            {
                //错误日志
                Models.ErrorLog.Add(_view_Admin_Info.UserID, "/SysBase/SysNDelete", ex.Message, _view_Admin_Info.Name);
                return Content(ConfigHelper.getErrorMsg);
            }
        }
        #endregion


        #region 辅助方法
        //递归树
        private void getTreeSysNav(List<DingTalkAgent.Model.SysNav> list, DingTalkAgent.Model.SysNav model, int parentID)
        {
            var l = list.Where(m => m.Parent_ID == parentID).ToList();
            if (model.Parent_ID == 0 && l.Count() > 0)
            {
                model.state = "closed";
            }
            foreach (var m in l)
            {
                m.id = m.Nav_ID;
                m.text = m.Name;
                getTreeSysNav(list, m, m.Nav_ID);
            }
            model.children = l;
        }
        #endregion
    }
}